package com.suning.sawp.service.impl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.companymem.CompanyMemBaseInfo;
import com.suning.sawp.dto.companymem.CompanyMemRecordInfo;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.service.util.DateUtils;

/**
 * 已销未提Dao
 *
 * @author 15050769
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("companyMemDaoService")
public class CompanyMemDaoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CompanyMemDaoService.class);

    @Resource
    DalClient dalClient;

    private final String SAVE_COMPANY_MEM_RECORD = "MDMCITY.SAVE_COMPANY_MEM_RECORD";

    private final String QUERY_RECORD_BYPARM = "MDMCITY.QUERY_RECORD_BYPARM";

    private final String DELETE_RECORD = "MDMCITY.DELETE_RECORD";

    private final String UPDATE_RECORD = "MDMCITY.UPDATE_RECORD";

    public boolean submitBaseInfo(CompanyMemBaseInfo companyMemBaseInfo, StoreManDto storeManDto) {
        boolean result = false;
        if (companyMemBaseInfo == null || storeManDto == null) {
            return false;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("CUST_NUM", companyMemBaseInfo.getCustNum());
        paramMap.put("CUST_CARD_NUM", companyMemBaseInfo.getCustCardNum());
        paramMap.put("STAFF_ID", storeManDto.getStaffId());
        paramMap.put("STORE_CODE", storeManDto.getStoreCode());
        paramMap.put("CUST_NAME", companyMemBaseInfo.getCustName());
        paramMap.put("COMPANY_NAME", companyMemBaseInfo.getCompanyName());
        paramMap.put("AREA_CD", storeManDto.getRegionCode());
        paramMap.put("BRANCH_CODE", storeManDto.getBranchCode());
        paramMap.put("CUST_MOBILE", companyMemBaseInfo.getCustMobile());

        try {
            int returnResult = dalClient.execute(SAVE_COMPANY_MEM_RECORD, paramMap);
            if (returnResult != 1) {
                result = false;
            } else {
                result = true;
            }
        } catch (Exception e) {
            LOGGER.error("queryCityInfo error ", e);
            return false;
        }
        return result;
    }

    public List<CompanyMemRecordInfo> queryRecordByParm(String dateParm, String queryParm, int index, int size,
            StoreManDto storeManDto) {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("index", index);
        paramMap.put("size", size);
        paramMap.put("staffId", storeManDto.getStaffId());
        paramMap.put("storeCode", storeManDto.getStoreCode());

        if (StringUtils.isNotBlank(dateParm)) {
            paramMap.put("lastDateParm", DateUtils.getNextMonthStr(dateParm, DateUtils.DEFAULT_MONTH_PATTERN, 1));
            paramMap.put("dateParm", dateParm);
        }

        if (StringUtils.isNotBlank(queryParm)) {
            paramMap.put("queryParm", "%" + queryParm + "%");
        }

        List<CompanyMemRecordInfo> resultList = dalClient.queryForList(QUERY_RECORD_BYPARM, paramMap,
                CompanyMemRecordInfo.class);
        return resultList;
    }

    public int deleteRecord(String recordId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("recordId", recordId);
        return dalClient.execute(DELETE_RECORD, paramMap);
    }

    public void updateRecord(String custNum) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("custNum", custNum);
            dalClient.execute(UPDATE_RECORD, paramMap);
        } catch (Exception e) {
            LOGGER.error("updateRecord exception custNum:" + custNum, e);
        }
    }
}
